import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import User from '../components/User/User'
import Rights from '../components/Power/rights'
import Roles from '../components/Power/Roles'
import Categories from '../components/Goods/Categories'
import Params from '../components/Goods/Params'
import List from '../components/Goods/List'
import AddGood from '../components/Goods/AddGood'
import Order from '../components/Order/Order'
import Report from '../components/Report/report'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      children: [
        { path: '/home', redirect: '/welcome' },
        { path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: AddGood },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
